package com.bsj.power.common.config.cached;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.bsj.power.common.config.redis.RedisConstant;
import com.bsj.power.common.constant.UWBPosConstant;
import com.bsj.power.common.def.dto.cardReport.BindCardDTO;
import com.bsj.power.common.def.vo.pls.httpVo.HttpTagPosVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import redis.clients.bsj.JedisClusterBSJ;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description BindCardCached
 * @time 2024/5/8 14:29
 */
@Component
public class BindCardCached {

    @Autowired
    private JedisClusterBSJ jedisClusterBSJ;

    /**
     * 获取绑卡信息
     *
     * @param str 设备号或工号
     * @return com.bsj.power.common.def.dto.cardReport.BindCardDTO
     * @author ljx
     * @time 2024/5/8 14:19
     */
    public BindCardDTO hgetBindCardInfo(String str) {
        String bindCardStr = jedisClusterBSJ.hget(RedisConstant.BIND_CARD_REDIS_KEY, str);
        if (bindCardStr == null) {
            return null;
        }
        return JSON.parseObject(bindCardStr, BindCardDTO.class);
    }

    /**
     * 缓存绑卡信息
     *
     * @param bindCardDTO
     * @return void
     * @author ljx
     * @time 2024/5/8 14:23
     */
    public void hsetBindCardInfo(BindCardDTO bindCardDTO) {
        if (bindCardDTO != null) {
            String jsonString = JSON.toJSONString(bindCardDTO);
            jedisClusterBSJ.hset(RedisConstant.BIND_CARD_REDIS_KEY, RedisConstant.BIND_CARD_REDIS_EMP_NO_KEY + bindCardDTO.getEmpNo(), jsonString);
            jedisClusterBSJ.hset(RedisConstant.BIND_CARD_REDIS_KEY, RedisConstant.BIND_CARD_REDIS_DEVICE_NAME_KEY + bindCardDTO.getDeviceName(), jsonString);
        }
    }

    /**
     * 删除绑卡信息
     *
     * @param empNo
     * @param deviceName
     * @return void
     * @author ljx
     * @time 2024/5/8 14:26
     */
    public void hdelBindCardInfo(String empNo, String deviceName) {
        jedisClusterBSJ.hdel(RedisConstant.BIND_CARD_REDIS_KEY, RedisConstant.BIND_CARD_REDIS_EMP_NO_KEY + empNo,
                RedisConstant.BIND_CARD_REDIS_DEVICE_NAME_KEY + deviceName);
    }

    /**
     * 根据人员工号查询位置信息
     *
     * @param empNos 工号集
     * @return
     */
    public Map<String, HttpTagPosVO> hGetEmpNoBindPos(List<String> empNos) {
        if (empNos.isEmpty()) {
            return null;
        }
        //BIND_CARD_REDIS_EMP_NO_KEY
        List<String> hmget = jedisClusterBSJ.hmget(RedisConstant.BIND_CARD_REDIS_KEY,
                empNos.stream().map(item -> RedisConstant.BIND_CARD_REDIS_EMP_NO_KEY + item).toArray(String[]::new));
        if (hmget == null) {
            return null;
        }
        Map<String, HttpTagPosVO> result = new HashMap<>();
        hmget.stream().forEach(item -> {
            BindCardDTO bindCardDTO = JSON.parseObject(item, BindCardDTO.class);
            if (bindCardDTO != null && bindCardDTO.getType() == 1) {
                String posStr = jedisClusterBSJ.get(UWBPosConstant.REDIS_TAG_POS_KEY + bindCardDTO.getDeviceName());
                if (posStr != null) {
                    HttpTagPosVO httpTagPosVO = JSON.parseObject(posStr, HttpTagPosVO.class);
                    if (httpTagPosVO != null) {
                        result.put(bindCardDTO.getEmpNo(), httpTagPosVO);
                    }
                }
            }
        });
        return result;
    }

    /**
     * 根据工号批量获取绑卡信息
     *
     * @param empNos
     * @return
     */
    public List<BindCardDTO> hmGetBindCardInfo(List<String> empNos) {
        List<String> hmget = jedisClusterBSJ.hmget(RedisConstant.BIND_CARD_REDIS_KEY,
                empNos.stream().map(item -> RedisConstant.BIND_CARD_REDIS_EMP_NO_KEY + item).toArray(String[]::new));
        if (hmget == null) {
            return null;
        }
        List<BindCardDTO> bindCardDTOS = new ArrayList<>();
        hmget.stream().forEach(item -> {
            if (item == null) {
                return;
            }
            BindCardDTO bindCardDTO = JSON.parseObject(item, BindCardDTO.class);
            bindCardDTOS.add(bindCardDTO);
        });
        return bindCardDTOS;
    }

    /**
     * 根据设备号批量获取绑卡信息
     *
     * @param deviceNames
     * @return
     */
    public List<BindCardDTO> hmGetBindCardInfoByDeviceNames(List<String> deviceNames) {
        List<String> hmget = jedisClusterBSJ.hmget(RedisConstant.BIND_CARD_REDIS_KEY,
                deviceNames.stream().map(item -> RedisConstant.BIND_CARD_REDIS_DEVICE_NAME_KEY + item).toArray(String[]::new));
        if (hmget == null) {
            return null;
        }
        List<BindCardDTO> bindCardDTOS = new ArrayList<>();
        hmget.stream().forEach(item -> {
            if (item == null) {
                return;
            }
            BindCardDTO bindCardDTO = JSON.parseObject(item, BindCardDTO.class);
            bindCardDTOS.add(bindCardDTO);
        });
        return bindCardDTOS;
    }

    /**
     * 获取所有绑卡信息（去重后的数据）
     *
     * @return java.util.List<com.bsj.power.common.def.dto.cardReport.BindCardDTO>
     * @author ljx
     * @time 2024/12/18 19:43
     */
    public List<BindCardDTO> hgetAllBindCard() {
        Map<String, String> map = jedisClusterBSJ.hgetAll(RedisConstant.BIND_CARD_REDIS_KEY);
        if (CollectionUtils.isEmpty(map)) {
            return null;
        }
        return map.values().stream().map(str -> {
            return JSONObject.parseObject(str, BindCardDTO.class);
        }).collect(Collectors.toMap(BindCardDTO::getDeviceName, a -> a, (k1, k2) -> k1)).values().stream().collect(Collectors.toList());
    }
}
